package org.demo.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;

public class MainfestUtil {

    //获取当前运行路径
    public static File getRunPath() throws UnsupportedEncodingException {
        URL location = MainfestUtil.class.getProtectionDomain().getCodeSource().getLocation();
        String path = URLDecoder.decode(location.getPath(), CharsetUtil.UTF_8);
        // 如果是 JAR 文件，获取其所在目录
        if (path.endsWith(".jar")) {
            path = StrUtil.subBefore(path, File.separator, true);
        }
        return FileUtil.file(path);
    }
}
